package com.barudisshu.controller;

import com.barudisshu.domain.Storage;
import com.barudisshu.exception.CustomError;
import com.barudisshu.model.FileMeta;
import com.barudisshu.service.StorageService;
import com.barudisshu.utils.AsyncScalr;
import com.barudisshu.utils.Scalr;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.RescaleOp;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@PropertySource("classpath:application.properties")
public class RestUploadController {

    private CustomError fileNotFound = new CustomError("0x000100", "Please select a file!");

    @Autowired
    private StorageService storageService;

    //Single file upload
    @PostMapping("/file")
    @ResponseBody
    public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile uploadFile) {
        if (uploadFile.isEmpty()) {
            return new ResponseEntity<>(fileNotFound, HttpStatus.OK);
        }
        try {
            List<String> filenames = storageService.saveUploadedFiles(Collections.singletonList(uploadFile));

            return new ResponseEntity<>(new FileMeta(filenames), new HttpHeaders(),
                    HttpStatus.OK);
        } catch (IOException e) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

    }

    // Multiple file upload
    @PostMapping("/file/multi")
    @ResponseBody
    public ResponseEntity<?> uploadFileMulti(@RequestParam("files") MultipartFile[] uploadFiles) {
        List<String> uploadedFileName = Arrays.stream(uploadFiles).map(MultipartFile::getOriginalFilename).filter(x ->
                !StringUtils.isEmpty(x)).collect(Collectors.toList());
        if (uploadedFileName.isEmpty()) {
            return new ResponseEntity<>(fileNotFound, HttpStatus.OK);
        }
        try {
            List<String> filenames = storageService.saveUploadedFiles(Arrays.asList(uploadFiles));
            return new ResponseEntity<>(new FileMeta(filenames), HttpStatus.OK);
        } catch (IOException e) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

    }

    @RequestMapping(value = "/file/{filename:.+}" ,method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<?> downloadFile(@PathVariable("filename") String filename) throws Exception {
        return storageService.download(filename, null, null);
    }

    @RequestMapping(value = "/file/{width}x{height}/{filename:.+}" ,method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<?> downloadScaleFile(
            @PathVariable("filename") String filename,
            @PathVariable("width") Integer width,
            @PathVariable("height") Integer height
    ) throws Exception {
        return storageService.download(filename, width, height);
    }


}
